﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Text;
using System.Text.RegularExpressions;

namespace DataAccessObject
{
    public class Pagination<T> where T : class
    {
        public Pagination()
        {
            items = new List<T>();
            total = 0;
            pageIndex = 1;
            pageSize = 15;
        }
        //pageBtnCount
        /// <summary>
        /// 分页数据
        /// </summary>
        public IList<T> items { set; get; }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int total { set; get; }

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int pageSize { set; get; }
        /// <summary>
        /// 指定当前页数，索引从1开始，默认第1页
        /// </summary>
        public int pageIndex { set; get; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int pageCount
        {
            set
            {
                pageCount = value;
            }
            get
            {
                //计算页数
                if (total < 1 || pageSize < 1)
                {
                    return 0;
                }
                int count = total / pageSize;
                if (count < 1)
                {
                    return 1;
                }
                if (total % pageSize > 0)
                {
                    count += 1;
                }
                return count;
            }
        }

        #region 显示分页
        /// <summary>
        /// 显示分页按钮
        /// <ul class="pagination">
        /// </summary>
        /// <param name="url">HttpContext.Current.Request.Url.ToString()</param>
        /// <param name="pageParameter">分页参数</param>
        /// <param name="pageBtnCount">显示按钮数</param>
        /// <param name="showInfo">是否显示分页信息</param>
        /// <returns></returns>
        public string ShowPagination(string url, string pageParameter = "pageIndex", int pageBtnCount = 7, bool showInfo = false)
        {
            #region 初始化URL
            string pageFormat = pageParameter + @"={0}";
            string strRegex = pageParameter + @"=\d*"; //正则表达式 "pageIndex=\d*"
            //string url = HttpContext.Current.Request.Url.ToString();
            if (pageCount <= 1)
            {
                return string.Empty;
            }

            if (Regex.IsMatch(url, strRegex))//如果URL包含分页参数 则替换
            {
                Regex rg = new Regex(strRegex);
                url = rg.Replace(url, pageFormat);
            }
            else//如果URL不包含分页参数 则添加
            {
                if (Regex.IsMatch(url, @"\?"))
                {
                    url = url + @"&" + pageFormat;
                }
                else
                {
                    url = url + @"?" + pageFormat;
                }
            }
            #endregion

            StringBuilder html = new StringBuilder();

            string prevBtn = "<li><a href=\"" + string.Format(url, (pageIndex - 1).ToString()) + "\">&laquo;上一页</a></li>";
            string nextBtn = "<li><a href=\"" + string.Format(url, (pageIndex + 1).ToString()) + "\">下一页&raquo;</a></li>";
            string firstBtn = "<li><a href=\"" + string.Format(url, "1") + "\">1</a></li>";
            string lastBtn = "<li><a href=\"" + string.Format(url, pageCount.ToString()) + "\">" + pageCount.ToString() + "</a></li>";

            if (pageIndex <= 1)
            {
                prevBtn = "<li class=\"disabled\"><a>&laquo;上一页</a></li>";
            }
            if (pageIndex >= pageCount)
            {
                nextBtn = "<li class=\"disabled\"><a>下一页&raquo;</a></li>";
            }
            if (pageIndex == 1)
            {
                firstBtn = "<li class=\"disabled\"><a>1</a></li>>";
            }
            if (pageIndex == pageCount)
            {
                lastBtn = "<li class=\"disabled\"><a>" + pageCount.ToString() + "</a></li>";
            }

            //----------------------------------------------------------
            //pageStr.Append("<span>共" + totalCount.ToString() + "条记录</span>");
            html.Append(prevBtn);

            if (pageCount <= pageBtnCount)//上一页  1  2  3  4  5  6  7  下一页
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    if (i == pageIndex)
                    {
                        html.Append("<li class=\"active\"><a>" + i + "</a></li>");
                    }
                    else
                    {
                        html.Append("<li><a href=\"" + string.Format(url, i.ToString()) + "\">" + i + "</a></li>");
                    }
                }
            }
            else
            {
                if (pageIndex <= (pageBtnCount - 1) / 2 + 1)
                { //上一页 1  2  3  4  5  6  7 ... 下一页
                    for (int i = 1; i <= pageBtnCount; i++)
                    {
                        if (i == pageIndex)
                        {
                            html.Append("<li class=\"active\"><a>" + i + "</a></li>");
                        }
                        else
                        {
                            html.Append("<li><a href=\"" + string.Format(url, i.ToString()) + "\">" + i + "</a></li>");
                        }
                    }
                    html.Append("<li><a>...</a></li>");
                    html.Append(lastBtn);
                }
                else if (pageIndex <= pageCount && pageIndex >= pageCount - (pageBtnCount - 1) / 2)
                {// 上一页  ... 2  3  4  5  6  7  8  下一页
                    html.Append(firstBtn);
                    html.Append("<li><a>...</a></li>");
                    for (int i = pageCount - pageBtnCount + 1; i <= pageCount; i++)
                    {
                        if (i == pageIndex)
                        {
                            html.Append("<li class=\"active\"><a>" + i + "</a></li>");
                        }
                        else
                        {
                            html.Append("<li><a href=\"" + string.Format(url, i.ToString()) + "\">" + i + "</a></li>");
                        }
                    }
                }
                else
                {// 上一页  ...  4  5  6  7  8  9  10 ...  下一页  当前页面居中显示
                    html.Append(firstBtn);
                    html.Append("<li><a>...</a></li>");
                    for (int i = pageIndex - (pageBtnCount - 1) / 2; i < (pageIndex + pageBtnCount - (pageBtnCount - 1) / 2); i++)
                    {
                        if (i == pageIndex)
                        {
                            html.Append("<li class=\"active\"><a>" + i + "</a></li>");
                        }
                        else
                        {
                            html.Append("<li><a href=\"" + string.Format(url, i.ToString()) + "\">" + i + "</a></li>");
                        }
                    }
                    html.Append("<li><a>...</a></li>");
                    html.Append(lastBtn);
                }

            }

            html.Append(nextBtn);

            if (showInfo)
            {
                string infoFormat = @"<div class=""pagination-info"">当前显示{0} ~ {1} 总共 {2} 条记录</div>"; //{start} ~ {end} 总共 {total} 
                var startNum = (this.pageIndex * this.pageSize);
                var endNum = (this.pageIndex) * this.pageSize;
                endNum = endNum >= this.total ? this.total : endNum;
                html.Append(string.Format(infoFormat, startNum, endNum, total));
            }
            return html.ToString();
        }

        /// <summary>
        ///显示上一页和下一页的简单翻页
        /// <ul class="pager">
        /// </summary>
        /// <param name="url">HttpContext.Current.Request.Url.ToString()</param>
        /// <param name="pageParameter">分页参数</param>
        /// <param name="IsCenter">是否居中</param>
        /// <returns></returns>
        public string ShowPager(string url, string pageParameter = "pageIndex", bool IsCenter = true)
        {
            #region 初始化URL
            string pageFormat = pageParameter + @"={0}";
            string strRegex = pageParameter + @"=\d*"; //正则表达式 "pageIndex=\d*"
            if (pageCount <= 1)
            {
                return string.Empty;
            }
            if (Regex.IsMatch(url, strRegex))//如果URL包含分页参数 则替换
            {
                Regex rg = new Regex(strRegex);
                url = rg.Replace(url, pageFormat);
            }
            else//如果URL不包含分页参数 则添加
            {
                if (Regex.IsMatch(url, @"\?"))
                {
                    url = url + @"&" + pageFormat;
                }
                else
                {
                    url = url + @"?" + pageFormat;
                }
            }
            #endregion
            StringBuilder html = new StringBuilder();
            string prevBtn = string.Empty;
            string nextBtn = string.Empty;

            string clazz = string.Empty;//previous|next
            string disabled = string.Empty;//disabled/active

            prevBtn = "<li><a class=\"{0}\" href=\"" + string.Format(url, (pageIndex - 1).ToString()) + "\">&laquo;上一页</a></li>";
            nextBtn = "<li><a class=\"{0}\" href=\"" + string.Format(url, (pageIndex + 1).ToString()) + "\">下一页&raquo;</a></li>";

            if (pageIndex <= 1)
            {
                prevBtn = "<li class=\"{0} disabled\"><a>&laquo;上一页</a></li>";
            }
            if (pageIndex >= pageCount)
            {
                nextBtn = "<li class=\"{0} disabled\"><a>下一页&raquo;</a></li>";
            }

            if (IsCenter)
            {
                prevBtn = string.Format(prevBtn, "");
                nextBtn = string.Format(nextBtn, "");
            }
            else
            {
                prevBtn = string.Format(prevBtn, "previous");
                nextBtn = string.Format(nextBtn, "next");
            }

            html.Append(prevBtn);
            html.Append(nextBtn);

            return html.ToString();
        }

        #endregion
    }
}